package com.wude.guild.holder;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.wude.bean.CouponListBean;
import com.wude.bean.ReceiveBean;
import com.wude.bean.UserInfo;
import com.wude.guild.R;
import com.wude.guild.dialog.GetCouponDialog;
import com.wude.http.HttpCom;
import com.wude.tools.TimeUtil;
import com.wude.tools.Utils;

import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 描述：优惠券Holder
 * 时间: 2017-09-04 14:50
 */
public class CouponHolder extends BaseHolder<CouponListBean.DataBean> {
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.btn_get)
    LinearLayout btnGet;

    private final String TAG = "CouponHolder";
    private CouponListBean.DataBean data = new CouponListBean.DataBean();
    private Activity activity;

    @Override
    protected void refreshView(CouponListBean.DataBean dataBean, int position, Activity activity) {
        this.data = dataBean;
        this.activity = activity;
        String startTime = TimeUtil.GetTime(dataBean.getUse_time_beg());
        String endTime = TimeUtil.GetTime(dataBean.getUse_time_end());
        if (dataBean.getUse_time_beg().equals("0")) {
            startTime = "无限期";
        }
        if (dataBean.getUse_time_end().equals("0")) {
            endTime = "无限期";
        }
        if (dataBean.getIs_new().equals("1") && dataBean.getIs_newapp().equals("1")) {
            tvHint.setText("新注册用户可用，限新版本客户端可用");
        } else if (dataBean.getIs_new().equals("1")) {
            tvHint.setText("新注册用户可用");
        } else if (dataBean.getIs_newapp().equals("1")) {
            tvHint.setText("限新版本客户端可用");
        } else {
            tvHint.setText("无使用限制");
        }
        tvTime.setText("使用期限:" + startTime + " ~ " + endTime);
        tvTitle.setText("《" + dataBean.getGame_name() + "》" + dataBean.getCoupon_class());
        tvPrice.setText(dataBean.getValue());
    }

    @Override
    protected View initView() {
        View inflate = LinearLayout.inflate(x.app(), R.layout.holder_coupon, null);
        ButterKnife.bind(this, inflate);
        inflate.setTag(this);
        return inflate;
    }

    @OnClick({R.id.btn_get})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_get:
                Map<String, String> map = new HashMap<>();
                UserInfo info = Utils.getLoginUser();
                map.put("token", info.token);
                map.put("id", data.getId());
                HttpCom.POST(getCouponHandler, HttpCom.getGetCoupon(), map, false);
                break;
        }
    }

    /**
     * 领取优惠券
     */
    private Handler getCouponHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    try {
                        Log.e(TAG + "领取优惠券返回的数据:", msg.obj.toString());
                        ReceiveBean bean = new Gson().fromJson(msg.obj.toString(), ReceiveBean.class);
                        if (bean.getStatus() == 1) {
                            new GetCouponDialog(activity, R.style.MillionDialogStyle).show();
                        } else {
                            ToastUtils.showShortToast(bean.getReturn_code());
                        }
                    } catch (Exception e) {
                        ToastUtils.showShortToast("数据解析异常");
                        Log.e(TAG + "领取优惠券异常：", e.toString());
                    }
                    break;
                case 2:
                    ToastUtils.showShortToast("网络错误");
                    break;
            }
        }
    };

}
